﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Windows.Forms;
using System.ComponentModel;

namespace schedule
{
    class likeCal : TableLayoutPanel
    {
        DateTime todayTime;
        DateTime viewTime;

        public List<TextBox> TB = new List<TextBox>();

        public likeCal()
        {
        }

        public void initDate(DateTime dateTime)
        {
            todayTime = dateTime;
        }
 
        public void setRowColumns(int rows, int columns)
        {
            ColumnCount = columns;
            RowCount = rows+1;
            for (int i = 0; i < RowCount; i++) RowStyles.Add(new RowStyle(SizeType.Percent, 100F));           
            for (int i = 0; i < ColumnCount; i++) ColumnStyles.Add(new ColumnStyle(SizeType.Percent,100F));

            int cnt = 0;
            for (int i = 0; i < RowCount; i++)
            {
                for (int j = 0; j < ColumnCount; j++)
                {
                    TextBox tb = new TextBox();
                    tb.Multiline = true;
                    tb.Enabled = false;
                    tb.BorderStyle = BorderStyle.None;
                    tb.Margin = new Padding(1);
                    tb.Dock = DockStyle.Fill;
                    tb.TextAlign = HorizontalAlignment.Center;
                    tb.BackColor = System.Drawing.Color.Empty;
                    tb.ReadOnly = true;

                    tb.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tb_MouseDown);

                    TB.Add(tb);
                    Controls.Add(TB[cnt++], j, i);
                }
            }
        }

        private int getTextBoxIndex(int day, int dayOfWeek)
        {
            int step = 7;
            return day + dayOfWeek-1 + step;
        }

        public void setMonth(DateTime DT)
        {
            clearDay();
            viewTime = DT;

            DateTime Start = new DateTime(DT.Year, DT.Month, 1);
            int End = DateTime.DaysInMonth(DT.Year, DT.Month);
            int week = (int)Start.DayOfWeek;

            // 요일 세팅
            string[] daysName = {"일", "월", "화", "수", "목", "금", "토"};
            for (int i = 0; i < 7; i++)
            {
                TB[i].Enabled = true;
                TB[i].Text = daysName[i];
                TB[i].BackColor = System.Drawing.Color.LightBlue;
                if (daysName[i] == "일")
                {
                    TB[i].ForeColor = System.Drawing.Color.Red;
                }
                else if (daysName[i] == "토")
                {
                    TB[i].ForeColor = System.Drawing.Color.Blue;
                }
            }

            // 날짜 세팅
            WorkManager wm = WorkManager.Instance();
            int step = 7;
            for (int day = 1 ; day <= End; day++)
            {
                TextBox tb = TB[getTextBoxIndex(day, week)];
                tb.Enabled = true;
                tb.Text = string.Format("{0:0}일", day);
                tb.BackColor = System.Drawing.Color.White;

                // 오늘은 핑크색.
                if (todayTime.Year == DT.Year && todayTime.Month == DT.Month && todayTime.Day == day)
                {
                    tb.BackColor = System.Drawing.Color.LightPink;
                }

                // 할일 항목이 있을 경우 추가 하자.
                foreach (WorkInfo wi in wm.workinfoList)
                {
                    // 자기가 참가한 그룹(타인이면.. 참가했는지... 자기것이라면 무조건 뜸)을 체크 함.
                    if (wm.isMyGroup(wi) == false)
                    {
                        continue;
                    }

                    DateTime loopDate = new DateTime(viewTime.Year, viewTime.Month, day);
                    DateTime startDate = new DateTime(wi.startTime.Year, wi.startTime.Month, wi.startTime.Day);
                    DateTime endDate = new DateTime(wi.endTime.Year, wi.endTime.Month, wi.endTime.Day);
                    long selectTick = loopDate.Ticks;
                    long startTick = startDate.Ticks;
                    long endTick = endDate.Ticks;
                    if (startTick <= selectTick && selectTick <= endTick)
                    {
                        tb.Text += "\r\n" + wi.title;
                    }
                }
            }
        }

        public void refreshScreen()
        {
            setMonth(viewTime);
        }

        public void clearDay()
        {
            foreach (TextBox var in TB)
            {
                var.Enabled = false;
                var.Text = string.Empty;
                var.BackColor = System.Drawing.Color.Empty;
            }

        }

        private void clickDay(int day)
        {
            // TODO : call event
            // 그 날짜에 항목이 세팅 되어 있는지?
            EventList eventList = new EventList();
            eventList.setDate(new DateTime(viewTime.Year, viewTime.Month, day));
            eventList.ShowDialog();
            if (eventList.returnType == EventList.ReturnType.ReturnType_Make)
            {
                // refresh calendar
                refreshScreen();
            }
            else if (eventList.returnType == EventList.ReturnType.ReturnType_Modify)
            {
                // refresh calendar
                refreshScreen();
            }
            else
            {
            }
        }

        // 클릭 했을 시 이벤트
        private void tb_MouseDown(object sender, MouseEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            string day = tb.Text;
            if (day.Length > 1)
            {
                // 날짜 클릭시.
                int carriage_return‎ = day.IndexOf("\r\n");
                if (carriage_return >= 0)
                {
                    day = day.Remove(carriage_return-1, day.Length-carriage_return+1);
                }
                else
                {
                    day = day.Remove(day.Length - 1, 1);
                }
                int dayNum = Convert.ToInt32(day);
                clickDay(dayNum);
            }
            else
            {
                // 요일 클릭시.
            }
        }
    }
}

